<?php

namespace app\hotel\controller;

use think\Controller;

class Room extends Controller
{
    public function __construct(){
        parent::__construct();

        $this->RoomModel = model('Hotel.Room');

        $this->GuestModel = model('Hotel.Guest');

        $this->BusinessModel = model('Business.User');

        $this->CouponModel = model('Hotel.Coupon');

        $this->CouponListModel = model('Hotel.Couponlist');

        $this->OrderModel = model('Hotel.Order');

        $this->CollectModel = model('Hotel.Collect');
    }

    
    // 获取房间列表
    public function list(){
        if($this->request->isPost()){
            $page = $this->request->param('page',1,'trim');
            $keywords = $this->request->param('keywords','','trim');
            $busid = $this->request->param('busid',0,'trim');

            $limit = 10;
            $offset = ($page-1) * $limit;

            $where = [];

            if(!empty($keywords)){
                $where['name'] = ['LIKE',"%$keywords%"];
            }

            $room = $this->RoomModel
                ->where($where)
                ->order("id desc")
                ->limit($offset,$limit)
                ->select();

            if(!$room){
                $this->error('暂无房间');
                exit;
            }

            $room = collection($room)->toArray();
            // 查询用户 
            $business = $this->BusinessModel->find($busid);
            if($business){
                foreach($room as $key => &$item){
                    // 查询房间收藏状态
                    $where = [
                        'busid' => $business['id'],
                        'roomid' => $item['id']
                    ];
                    $collect = $this->CollectModel->where($where)->find();
                    if(!$collect){
                        $item['collect'] = false;
                    }else{
                        $item['collect'] = true;
                    }
                   
                }
            }

            $this->success('返回房间列表',null,$room);
            exit;
        }
    }


    // 获取房间详情
    public function info(){
        if($this->request->isPost()){
            $id = $this->request->param('id',0,'trim');
            
            // 查找房间
            $room = $this->RoomModel->find($id);
            if(!$room){
                $this->error('房间信息未知');
                exit;
            }else{
                $this->success('返回房间信息',null,$room);
                exit;
            }
        }
    }


    // 获取住户信息
    public function guest(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');

            // 查找用户是否存在
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            $guest = $this->GuestModel->where(['busid' => $busid])->select();

            if(!$guest){
                $this->error('暂无住户信息');
                exit;
            }else{
                $this->success('返回住户信息',null,$guest);
                exit;
            }
        }
    }

    
    // 获取优惠券信息
    public function coupon(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');

            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            $where = [
                'busid' => $busid,
                'couponlist.status' => '1'
            ];

            $receive = $this->CouponListModel
                ->with(['coupon'])
                ->where($where)
                ->select();
            
            if(!$receive){
                $this->error('暂无优惠券');
                exit;
            }else{
                $this->success('返回优惠券信息',null,$receive);
                exit;
            }
        }
    }


    // 预订酒店
    public function add(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            // 优惠券领取表中的id
            $cid = $this->request->param('cid',0,'trim');
            $roomid = $this->request->param('roomid',0,'trim');
            $oprice = $this->request->param('oprice',0,'trim');
            $price = $this->request->param('price',0,'trim');
            $starttime = $this->request->param('starttime',0,'trim');
            $endtime = $this->request->param('endtime',0,'trim');
            $guest = $this->request->param('guest',NULL,'trim');

            // 查找用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 查找房间
            $room = $this->RoomModel->find($roomid);
            if(!$room){
                $this->error('房间未知');
            }

            // 查看房间是否还有剩余
            $where = [
                'roomid' => $roomid,
                'status' => ['IN',[1,2]]
            ];

            $count = $this->OrderModel->where($where)->count();

            $updatecount = bcsub($room['total'],$count);

            if($updatecount <= 0){
                $this->error('该房型已全部被预约');
                exit;
            }

            // 查看优惠券是否可以使用
            $where = [
                'busid' => $busid,
                'couponlist.id' => $cid
            ];

            $receive = $this->CouponListModel->with(['coupon'])->where($where)->find();

            if($receive){
                if($receive['status'] == 2){
                    $this->error('该优惠券已使用');
                    exit;
                }elseif($receive['status'] == 3){
                    $this->error('该优惠券已过期');
                    exit;
                }
            }

            // 判断用户余额是否充足
            $updateMoney = bcsub($business['money'],$price);
            if($updateMoney < 0){
                $this->error('用户余额不足');
                exit;
            }

            // 开启事务
            // 订单表,优惠券领取表,用户表
            $this->OrderModel->startTrans();
            $this->CouponListModel->startTrans();
            $this->BusinessModel->startTrans();

            // 订单数据
            $OrderData = [
                'code' => build_code('HT'),
                'busid' => $busid,
                'roomid' => $roomid,
                'cid' => $cid ? $cid : NULL,
                'oprice' => $oprice,
                'price' => $price,
                'starttime' => $starttime,
                'endtime' => $endtime,
                'status' => 1,
                'guest' => $guest
            ];

            // 插入订单表
            $OrderStatus = $this->OrderModel->validate('common/Hotel/Order')->save($OrderData);

            if($OrderStatus === FALSE){
                $this->error($this->OrderModel->getError());
                exit;
            }

            // 用户表数据
            $BusinessData = [
                'id' => $busid,
                'deal' => 1,
                'money' => $updateMoney
            ];

            // 更新用户表
            $BusinessStatus = $this->BusinessModel->isUpdate(true)->save($BusinessData);

            if($BusinessStatus === FALSE){
                $this->OrderModel->rollback();
                $this->error($this->BusinessModel->getError());
                exit;
            }

            // 判断有没有用到优惠券
            if($OrderData['cid']){
                // 优惠券领取表数据
                $ReceiveData = [
                    'id' => $cid,
                    'status' => 2
                ];

                $ReceiveStatus = $this->CouponListModel->isUpdate(true)->save($ReceiveData);

                if($ReceiveData === FALSE){
                    $this->BusinessModel->rollback();
                    $this->OrderModel->rollback();
                    $this->error("预订房间失败,请重试");
                    exit;
                }
            }

            if($OrderStatus === FALSE || $BusinessStatus === FALSE){
                $this->CouponListModel->rollback();
                $this->BusinessModel->rollback();
                $this->OrderStatus->rollback();
                $this->error('预订房间失败,请重试');
                exit;
            }else{
                $this->CouponListModel->commit();
                $this->BusinessModel->commit();
                $this->OrderModel->commit();
                $this->success('预订房间成功');
                exit;
            }


        }
    }


    // 获取订单信息
    public function order(){
        if($this->request->isPost()){
            $roomid = $this->request->param('roomid',0,'trim');

            // 查询房间
            $room = $this->RoomModel->find($roomid);
            if(!$room){
                $this->error('房间未知');
                exit;
            }

            // 查询订单
            $order = $this->OrderModel->with(['business'])->where(['roomid' => $roomid])->select();

            if(!$order){
                $this->error('获取信息失败');
                exit;
            }else{
                $this->success('获取信息成功',null,$order);
                exit;
            }
        }
    }

}
